package com.health.mapp.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import com.health.db.model.CourseGoodsTemp;
import com.health.db.model.CourseList;
import com.health.db.model.CourseShareRemark;
import com.health.db.model.CourseShareRemarkExample;
import com.health.db.model.GoodsListExample;
import com.health.db.model.GoodsListTemp;
import com.health.db.model.GoodsOrder;
import com.health.db.model.UserLIST;
import com.health.db.service.sm.IGoodsListService;
import com.health.db.service.sm.IGoodsOrderService;
import com.health.db.service.user.ICourseShareRemarkService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryCourseGoodsReq;
import com.health.mapp.bean.QueryCourseGoodsResp;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;
import com.wxtools.api.IMiniService;
import com.wxtools.bean.WxUserList.WxUser;
import com.wxtools.bean.WxUserList.WxUser.WxUserGet;
/**
 * 查看课程类商品 (废弃)
 * 
 * @author Yuchong
 *
 */
public class QueryCourseGoodsListImpl implements ActionInterface {

	private IGoodsListService goodsListService  ;
	private IGoodsOrderService goodsOrderService;
	private IUserLISTService userLISTService;
    private IMiniService miniService;
    
    private ICourseShareRemarkService courseShareRemarkService;
    
    private static String GOODS_ID = "61";
    
    private static String freeFalg="1";
    
    
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		QueryCourseGoodsReq req = (QueryCourseGoodsReq) reqBean;
		QueryCourseGoodsResp _resp = new QueryCourseGoodsResp();
		//初始化用户id
		Long userId = 0l;
	    String openId = req.getOpenId();
	    String goodsId = GOODS_ID;
	    String inviterId = req.getInviterId();
	    //邀请链接
	    String path = req.getPath();
		if (StringUtils.isNotBlank(goodsId)&&StringUtils.isNotBlank(path)) {
			
			//userid入参
			if ( StringUtils.isNotBlank(req.getUserId())){
				userId= Long.valueOf(req.getUserId());
			}else if(StringUtils.isNotBlank(openId)){
				  List<UserLIST> userList= userLISTService.selectByWXOpenid(openId);
				  
				  if (userList.size()>0) {
					  userId = userList.get(0).getUserid();
				  }else{
					  _resp.setErrorCode(ResultCode.LOGIN_FAIL_MEMBER_IS_NOT_EXISTS);
					  _resp.setErrorMsg(ResultCode.LOGIN_FAIL_MEMBER_IS_NOT_EXISTS_NOTE);
					  return _resp;
				  }
				
			}else{
				_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
				return _resp;
			}
			
			//查看之前是否已分享
			CourseShareRemarkExample shareRemarkExample = new CourseShareRemarkExample();
			shareRemarkExample.setGoodsId(Long.valueOf(goodsId));
			shareRemarkExample.setUserId(Long.valueOf(userId));
			List<CourseShareRemark> shareRemarkList =courseShareRemarkService.selectByExample(shareRemarkExample);
			
			if (shareRemarkList.size()>0) {
				//取之前生成过的永久链接
				_resp.setMiNiShareUrl(shareRemarkList.get(0).getShareImageUrl());
				_resp.setMiNiShareQRUrl(shareRemarkList.get(0).getShareImageUrl());
			}else{
				//新生成链接
				String shareImageUrl = miniService.createwxaqrcode(path, "430");
				
				//test start 
				
				WxUserGet userGet = new WxUserGet();
				
				userGet.setOpenid(openId);
				
				WxUser wxUser = miniService.getUserInfoByOpenId(userGet);
				
				System.out.println(wxUser.toString());
				
				//test end
				
				//存储
				CourseShareRemark  courseShareRemarkBean = new CourseShareRemark();
				courseShareRemarkBean.setGoodsId(Long.valueOf(goodsId));
				courseShareRemarkBean.setUserId(Long.valueOf(userId));
				courseShareRemarkBean.setShareImageUrl(shareImageUrl);
				
				//插入邀请人
				if (StringUtils.isNotBlank(inviterId)) {
					courseShareRemarkBean.setInviterId(Long.valueOf(inviterId));
				}
			
				courseShareRemarkService.insert(courseShareRemarkBean);
				_resp.setMiNiShareUrl(shareImageUrl);
				_resp.setMiNiShareQRUrl(shareImageUrl);
			}
			
			GoodsListExample example = new GoodsListExample();
			example.setGoodsid(Long.valueOf(goodsId));
			//查询订单
			List<GoodsListTemp> list =  goodsListService.selectBySimple(example);
			 CourseGoodsTemp courseGoods = new CourseGoodsTemp();
			if (list.size()==1) {
				GoodsListTemp temp = list.get(0);
			    BeanUtils.copyProperties(temp,courseGoods);
			   // courseGoods.setCourseList(packagecourseList());
			   // courseGoods.setDetailPicList(packageDetailList());
			}else{
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3018);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3018_NOTE);
			}
			
			List<GoodsOrder>  goodsOrderList =goodsOrderService.selectByUserId(userId,Long.valueOf(goodsId));
			//判断是否购买成功
			courseGoods.setIsOrder("1");
			/*if (goodsOrderList.size()==1) {
				courseGoods.setIsOrder("1");
			}*/
			_resp.setCourseGoods(courseGoods);
			
		}else{
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		//分享页面头像
		_resp.setMiNiSaherIconUrl("video/60/image/icon.png");
		
		_resp.setMiNiShareLabelUrl("video/60/image/label.png");
		return _resp;
	}
	
	//返回详情列表
	private List<String> packageDetailList(){
		List<String> list = new  ArrayList<String>();
		
		list.add("video/60/image/1.png");
		list.add("video/60/image/2.png");
		list.add("video/60/image/3.png");
		list.add("video/60/image/4.png");
		list.add("video/60/image/5.png");
		list.add("video/60/image/6.png");
		return list;		
	}
	
	//返回视屏列表
	private List<CourseList>  packagecourseList(){
		List<CourseList> list = new ArrayList<CourseList>();
		CourseList  course1 = new  CourseList();
		course1.setTitle("流感和普通感冒的区别");
		course1.setPalyTime("时长2分3秒");
		course1.setSouceUrl("video/60/1.mp4");
		
		CourseList  course2 = new  CourseList();
		course2.setTitle("病毒感染的血常规表现");
		course2.setPalyTime("时长1分33秒");
		course2.setSouceUrl("video/60/2.mp4");
		
		CourseList  course3 = new  CourseList();
		course3.setTitle("流感的主要症状");
		course3.setPalyTime("时长2分20秒");
		course3.setSouceUrl("video/60/3.mp4");
		
		CourseList  course4 = new  CourseList();
		course4.setTitle("感冒吃什么药");
		course4.setPalyTime("时长2分08秒");
		course4.setSouceUrl("video/60/4.mp4");
		
		CourseList  course5= new  CourseList();
		course5.setTitle("宝宝病毒性感冒发烧怎么办");
		course5.setPalyTime("时长2分08秒");
		course5.setSouceUrl("video/60/5.mp4");
		
		
		CourseList  course6 = new  CourseList();
		course6.setTitle("病毒性感冒会传染吗");
		course6.setPalyTime("时长3分42秒");
		course6.setSouceUrl("video/60/6.mp4");
		
		
		CourseList  course7 = new  CourseList();
		course7.setTitle("病毒性感冒吃什么药");
		course7.setPalyTime("时长3分59秒");
		course7.setSouceUrl("video/60/7.mp4");
		
		
		CourseList  course8 = new  CourseList();
		course8.setTitle("病毒感冒和细菌感冒的区别");
		course8.setPalyTime("时长1分45秒");
		course8.setSouceUrl("video/60/8.mp4");
		
		
		CourseList  course9 = new  CourseList();
		course9.setTitle("刺激性干咳是怎么回事");
		course9.setPalyTime("时长2分38秒");
		course9.setSouceUrl("video/60/9.mp4");
		
		
		CourseList  course10= new  CourseList();
		course10.setTitle("孩子干咳怎么办");
		course10.setPalyTime("时长2分12秒");
		course10.setSouceUrl("video/60/10.mp4");
		
		
		CourseList  course11 = new  CourseList();
		course11.setTitle("做雾化对宝宝有伤害吗");
		course11.setPalyTime("时长2分56秒");
		course11.setSouceUrl("video/60/11.mp4");
		
		
		CourseList  course12 = new  CourseList();
		course12.setTitle("血项正常为什么还发烧");
		course12.setPalyTime("时长2分17秒");
		course12.setSouceUrl("video/60/12.mp4");
		
		
		CourseList  course13 = new  CourseList();
		course13.setTitle("血项高怎么治疗");
		course13.setPalyTime("时长1分18秒");
		course13.setSouceUrl("video/60/13.mp4");
		
		
		CourseList  course14 = new  CourseList();
		course14.setTitle("肺炎是什么原因引起的");
		course14.setPalyTime("时长1分34秒");
		course14.setSouceUrl("video/60/14.mp4");
		
		
		CourseList  course15 = new  CourseList();
		course15.setTitle("宝宝肺炎有什么症状");
		course15.setPalyTime("时长2分49秒");
		course15.setSouceUrl("video/60/15.mp4");
		
		CourseList  course16 = new  CourseList();
		course16.setTitle("肺炎怎么治疗最好");
		course16.setPalyTime("时长3分37秒");
		course16.setSouceUrl("video/60/16.mp4");
		
		
		list.add(course1);
		list.add(course2);
		list.add(course3);
		list.add(course4);
		list.add(course5);
		list.add(course6);
		list.add(course7);
		list.add(course8);
		list.add(course9);
		list.add(course10);
		list.add(course11);
		list.add(course12);
		list.add(course13);
		list.add(course14);
		list.add(course15);
		list.add(course16);
		
		return list;
	}
	
	
	
	
	public IGoodsListService getGoodsListService() {
		return goodsListService;
	}
	public void setGoodsListService(IGoodsListService goodsListService) {
		this.goodsListService = goodsListService;
	}
	public IGoodsOrderService getGoodsOrderService() {
		return goodsOrderService;
	}
	public void setGoodsOrderService(IGoodsOrderService goodsOrderService) {
		this.goodsOrderService = goodsOrderService;
	}
	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}
	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}

	public IMiniService getMiniService() {
		return miniService;
	}
	public void setMiniService(IMiniService miniService) {
		this.miniService = miniService;
	}

	public ICourseShareRemarkService getCourseShareRemarkService() {
		return courseShareRemarkService;
	}

	public void setCourseShareRemarkService(
			ICourseShareRemarkService courseShareRemarkService) {
		this.courseShareRemarkService = courseShareRemarkService;
	}
	
	
}
